/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.factor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.poll.common.constant.enums.PollSysDictEnum;
import com.hopes.bizz.poll.factor.dto.PollFactorInfoExcelDTO;
import com.hopes.bizz.poll.factor.entity.PollFactorInfoEntity;
import com.hopes.bizz.poll.factor.mapper.PollFactorInfoMapper;
import com.hopes.bizz.poll.factor.service.PollFactorInfoService;
import com.hopes.bizz.poll.factor.vo.PollFactorInfoExcelVO;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.core.exception.BizException;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 因子信息
 *
 * @author daijie
 * @date 2023-05-19 09:37:47
 */
@Service
public class PollFactorInfoServiceImpl extends ServiceImpl<PollFactorInfoMapper, PollFactorInfoEntity> implements PollFactorInfoService {

    private final String CODE_ERROR_MSG = "因子编码不允许重复!";

    @Override
    public ValidateImportBO<PollFactorInfoExcelDTO> validateImport(List<PollFactorInfoExcelDTO> excelDTOList) {
        ValidateImportBO<PollFactorInfoExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<PollFactorInfoExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        List<PollFactorInfoEntity> list = this.list();
        for (PollFactorInfoExcelDTO excelDTO : excelDTOList) {
            // 检验角色名称或者角色编码是否存在
            boolean exist = list.stream().anyMatch(item -> excelDTO.getCode().equals(item.getCode()));
            if (exist) {
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add(CODE_ERROR_MSG + "  " + excelDTO.getCode());
                }}));
                failedNum++;
            } else {
                saveList.add(excelDTO);
                successNum++;
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public boolean save(PollFactorInfoEntity entity) {
        for (PollFactorInfoEntity pollFactorInfoEntity : this.list()) {
            if (pollFactorInfoEntity.getCode().equals(entity.getCode())) {
                throw new BizException(CODE_ERROR_MSG);
            }
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(PollFactorInfoEntity entity) {
        for (PollFactorInfoEntity pollFactorInfoEntity : this.list()) {
            if (!pollFactorInfoEntity.getId().equals(entity.getId()) && pollFactorInfoEntity.getCode().equals(entity.getCode())) {
                throw new BizException(CODE_ERROR_MSG);
            }
        }
        return super.updateById(entity);
    }

    @Override
    public PollFactorInfoEntity importDtoToEntity(PollFactorInfoExcelDTO excelDto) {
        PollFactorInfoEntity entity= new PollFactorInfoEntity();
        BeanUtils.copyProperties(excelDto, entity);
        entity.setUseFlag(StringUtils.getUseFlag(excelDto.getUseFlag()));
        entity.setFactorType(DictResolver.getItemValueByLabel(PollSysDictEnum.factor_type, excelDto.getFactorType()));
        return entity;
    }

    @Override
    public PollFactorInfoExcelVO exportEntityToVo(PollFactorInfoEntity entity) {
        PollFactorInfoExcelVO excelVO = new PollFactorInfoExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        if (ObjectUtil.isNotNull(entity.getUseFlag())) {
            excelVO.setUseFlag(StringUtils.getUseFlagStr(entity.getUseFlag()));
        }
        excelVO.setFactorType(entity.getFactorTypeStr());
        return excelVO;
    }

    @Override
    public List<SysDictItem> labelValueList() {
        return baseMapper.listFactorInfo();
    }
}
